package com.bilicute.libman.controller;

import com.bilicute.libman.pojo.Result;
import com.bilicute.libman.pojo.VerifyCode;
import com.bilicute.libman.quickMethods.VerifyCodeMethods;
import com.bilicute.libman.service.IVerifyCodeGen;
import com.bilicute.libman.service.MailService;
import com.bilicute.libman.service.impl.IVerifyCodeGenImpl;
import com.bilicute.libman.util.RandomUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.Email;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

import static com.bilicute.libman.util.StringUtil.isEmptyString;

/**
 * @所属包名: com.bilicute.libman.controller
 * @类名: VerifyCodeController
 * @作者: i囡漫笔
 * @描述: 验证码发送
 * @创建时间: 2024-06-20 11:12
 */

@RestController
@CrossOrigin
@RequestMapping("/api/getCode")
@Validated
public class VerifyCodeController {
    @Autowired
    private MailService mailService;
    /**
     * @param request: 网络请求,用于Session存放
     * @param response: 网络响应，用于设置响应头
     * @param email: 邮箱
     * @return Result
     * @author i囡漫笔
     * @description 用于新用户绑定邮箱获取邮箱验证码时使用
     * @date 2024/4/18
     */
    @PostMapping("/mail")
    public Result<String> MailVerifyCode(HttpServletRequest request, HttpServletResponse response, @Email String email,String imgCode) {

        if (!VerifyCodeMethods.verifyByImg(request,imgCode)) {
            return Result.error("图形验证码错误");
        }
        //设置响应头
        response.setHeader("Pragma", "no-cache");
        //设置响应头
        response.setHeader("Cache-Control", "no-cache");
        //在代理服务器端防止缓冲
        response.setDateHeader("Expires", 0);
        if (request.getSession().getAttribute("RequestMailTime") != null) {
            if (System.currentTimeMillis()-(long)request.getSession().getAttribute("RequestMailTime") < 60000){
                return Result.error("60秒内请勿重复发送验证码");
            }
        }
        if (isEmptyString(email)){
            return Result.error("邮箱为空");
        }
        String code = RandomUtils.randomString(6);
        //将VerifyCode绑定session
        request.getSession().setAttribute("RequestMailTime",System.currentTimeMillis());
        request.getSession().setAttribute("Mail",email);
        request.getSession().setAttribute("MailCode", code);
        mailService.sendEmail(email, code, "用户");
        return Result.success();
    }

    @GetMapping("/img")
    public void verifyCode(HttpServletRequest request, HttpServletResponse response) {
        IVerifyCodeGen iVerifyCodeGen = new IVerifyCodeGenImpl();
        try {
            //设置长宽
            VerifyCode verifyCode = iVerifyCodeGen.generate(110, 30);
            String code = verifyCode.getCode();
//            LOGGER.info(code);
            //将VerifyCode绑定session
            request.getSession().setAttribute("VerifyCode", code);
//            System.out.println(request.getSession().getAttribute("VerifyCodeMethods"));
            //设置响应头
            response.setHeader("Pragma", "no-cache");
            //设置响应头
            response.setHeader("Cache-Control", "no-cache");
            //在代理服务器端防止缓冲
            response.setDateHeader("Expires", 0);
            //设置响应内容类型
            response.setContentType("image/jpeg");
            response.getOutputStream().write(verifyCode.getImgBytes());
            response.getOutputStream().flush();
        } catch (IOException e) {
            System.out.println("错误" + e);
        }
    }
}
